<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :destroyOnClose="true"
    cancelText="关闭"
  >
    <ProjectReview ref="projectForm" :dataId="projectData.id" :data="projectData">
      <h4 style="margin-top:15px;">指导教师审批</h4>
      <table border="1px" widht="100%">
        <tr>
          <td width="904px">
            <a-form-item style="margin-top:15px;" :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }" label="审批人">
              <x-select-user-by-dep v-model="workflowModel.handlerId" :multi="false"></x-select-user-by-dep>
            </a-form-item>
          </td>
        </tr>
      </table>
    </ProjectReview>

    <template slot="footer">
      <a-button key="cancel" @click="handleCancel">
        取消
      </a-button>
       <a-button key="save" type="primary" @click="handleSave">
        保存
      </a-button>
      <a-button key="submit" type="primary" @click="handleOk">
        确定
      </a-button>
    </template>
  </a-modal>
</template>

<script>
import ProjectReview from './ProjectReview'
import XSelectUserByDep from '@/components/extends/XSelectUserByDep'
import { setProject } from '@/api/api'
import moment from 'moment'
export default {
  name:'ProjectReviewModal',
  components: {
    ProjectReview,
    XSelectUserByDep
  },
  data() {
    return {
      title: '项目申报',
      width: 1000,
      visible: false,
      confirmLoading: false,
      model: {},
      projectData:{},
      workflowModel:{
        handlerId:'',
        operation:'start'
      },
      url: {
        config: '/bizConfig/list'
      }
    }
  },
  methods: {
    handleOk() {
      this.$refs.projectForm.form.validateFields((err, fieldsValue) => {
        if (err) {
          return this.$message.warning('必填项有误！')
        }
        
        let data = this.$refs.projectForm.form.getFieldsValue()
        data.researchProject.actStatus = 1
        data.researchProject.startTime = moment(data.researchProject.timeRange[0]).valueOf()
        data.researchProject.endTime = moment(data.researchProject.timeRange[1]).valueOf()
        delete data.researchProject.timeRange
        setProject(data).then(res=>{
          if(res.success){
            this.$message.success(res.message)
            this.visible = false
            this.$emit('ok',data)
          }else{
            this.$message.error(res.message)
          }
        })
      })
    },
    handleSave(){
        let data = this.$refs.projectForm.form.getFieldsValue()
        if(!data.researchProject.name){
          return this.$message.warning('项目名称不能为空！')
        }
        if(data.researchProject.timeRange){
          data.researchProject.startTime = moment(data.researchProject.timeRange[0]).valueOf()
          data.researchProject.endTime = moment(data.researchProject.timeRange[1]).valueOf()
        }
        delete data.researchProject.timeRange
        data.researchProject.actStatus = 0
        setProject(data).then(res=>{
          if(res.success){
            this.$message.success(res.message)
            this.visible = false
            this.$emit('ok',data)
          }else{
            this.$message.error(res.message)
          }
        })
    },
    handleCancel() {
      this.visible = false
    },
    handleAdd(){
      this.$refs.projectForm.title = '添加'
      this.$refs.projectForm.add();
    },
    add() {
      this.edit({})
    },
    edit(data) {
      this.projectData = data
      this.visible = true
    },
    onCellChange() {}
  }
}
</script>

<style scoped>
#staffLeaveTable .input .ant-input,
.staffLeaveTableId .ant-input {
  border: 0 solid #000 !important;
  border-radius: 0;
  display: inherit;
  background-color: #fff;
}
</style>
